######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figure2_3-plots.R
#  :: produces plots for slider results by marginality overall, and by incumbent party 

rm(list=ls())
setwd('~/Dropbox/rainReplication')   
           
load('slider/figure2-4_3.Rdata')

dprv_seq=c(0, 0.15, 0.25, 0.35, 0.45) 
 
# borrow information from the other model ...
ci1=cbind(coef1[,1]+coef1[,2]*1.64,coef1[,1]-coef1[,2]*1.64)
ci2=cbind(coef2[,1]+coef2[,2]*1.64,coef2[,1]-coef2[,2]*1.64)

pdf('slider/marginal_slider_all_day.pdf')                          
par(cex.axis=.9)
plot(coef1[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci1[i,1],ci1[i,2]),lty=2) 
}
axis(2)
lines(lowess(f=.6,coef1[,1]~dprv_seq),col='grey20',lty=2)
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()
   
pdf('slider/marginal_slider_all_weekend.pdf')                          
par(cex.axis=.9)
plot(coef2[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci2[i,1],ci2[i,2]),lty=2,col='grey20')	
}
axis(2)
lines(lowess(f=.6,coef2[,1]~dprv_seq),col='grey10',lty=2)   
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()
             
# demx

rm(list=ls())
setwd('~/Dropbox/rainReplication')   
           
load('slider/figure3_demx-4_3.Rdata')
 
dprv_seq=c(0, 0.15, 0.25, 0.35, 0.45)        
                   
# borrow information from the other model ...
ci1=cbind(coef1[,1]+coef1[,2]*1.64,coef1[,1]-coef1[,2]*1.64)
ci2=cbind(coef2[,1]+coef2[,2]*1.64,coef2[,1]-coef2[,2]*1.64)

pdf('slider/marginal_slider_demx_day.pdf')                          
par(cex.axis=.9)
plot(coef1[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci1[i,1],ci1[i,2]),lty=2) 
}
axis(2)
lines(lowess(f=.6,coef1[,1]~dprv_seq),col='grey20',lty=2)
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()

pdf('slider/marginal_slider_demx_weekend.pdf')                          
par(cex.axis=.9)
plot(coef2[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F    )
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci2[i,1],ci2[i,2]),lty=2,col='grey20')	
}
axis(2)
lines(lowess(f=.6,coef2[,1]~dprv_seq),col='grey10',lty=2)   
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()
       
# repx

rm(list=ls())
setwd('~/Dropbox/rainReplication')   
           
load('slider/figure3_repx-4_3.Rdata')

dprv_seq=c(0, 0.15, 0.25, 0.35, 0.45)                             
  
# borrow information from the other model ...
ci1=cbind(coef1[,1]+coef1[,2]*1.64,coef1[,1]-coef1[,2]*1.64)
ci2=cbind(coef2[,1]+coef2[,2]*1.64,coef2[,1]-coef2[,2]*1.64)

pdf('slider/marginal_slider_repx_day.pdf')                          
par(cex.axis=.9)
plot(coef1[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F)
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci1[i,1],ci1[i,2]),lty=2) 
}
axis(2)
lines(lowess(f=.6,coef1[,1]~dprv_seq),col='grey20',lty=2)
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()

pdf('slider/marginal_slider_repx_weekend.pdf')                          
par(cex.axis=.9)
plot(coef2[,1]~dprv_seq,main='',xlab='Absolute Presidential Vote Margins',ylab='IV Estimate',ylim=c(-5,10),col='grey20',pch=19,axes=F)
for(i in 1:nrow(ci1)){
	lines(x=c(dprv_seq[i],dprv_seq[i]),y=c(ci2[i,1],ci2[i,2]),lty=2,col='grey20')	
}
axis(2)
lines(lowess(f=.6,coef2[,1]~dprv_seq),col='grey10',lty=2)   
axis(1,at=c(dprv_seq),labels=dprv_seq)
dev.off()

# end